﻿using System;
using System.Collections.Generic;
using System.DrawingCore;
using System.DrawingCore.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using BCCommon;
using Microsoft.AspNetCore.Mvc;

namespace BCTMSRESTful.Controllers.Anonymous
{
    /// <summary>
    /// 二维码生成
    /// </summary>
    [Route("api/v1/[controller]")]
    public class QRCodeController : BCTMSRESTfulAPIController
    {
        /// <summary>
        /// 获取二维码图片
        /// 主要用于司机、用户、合伙人邀请新用户或副司机申请等功能使用
        /// </summary>
        /// <param name="contents">扫一扫内容</param>
        /// <returns></returns>
        [HttpGet("{contents}")]
        public IActionResult Get(string contents)
        {
            QRCodeHelper qrGenerator = new QRCodeHelper();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(HttpUtility.UrlDecode(contents), QRCodeHelper.ECCLevel.H);
            QRCode qrCode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrCode.GetGraphic(5);
            MemoryStream stream = new MemoryStream();
            qrCodeImage.Save(stream, ImageFormat.Png);
            return File(stream.ToArray(), "image/png");
        }
    }
}
